package xyz.cerberu.cet.subtitle.controller;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.cerberu.cet.global.annotation.JsonFilter;
import xyz.cerberu.cet.global.domain.Global;
import xyz.cerberu.cet.global.domain.Page;
import xyz.cerberu.cet.global.domain.Result;
import xyz.cerberu.cet.global.utils.ResultUtils;
import xyz.cerberu.cet.resource.domian.Resource;
import xyz.cerberu.cet.subtitle.domain.Subtitle;
import xyz.cerberu.cet.subtitle.exception.SubtitleNotExistException;
import xyz.cerberu.cet.subtitle.service.SubtitleServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author β世界
 * Created on 18:58
 * @Description 字幕组control
 */
@Controller
@RequestMapping("/subtitle")
public class SubtitleControl {
    private final SubtitleServiceImpl subtitleServiceImpl;
    private final Global global;

    @Autowired
    public SubtitleControl(SubtitleServiceImpl subtitleServiceImpl, Global global) {
        this.subtitleServiceImpl = subtitleServiceImpl;
        this.global = global;
    }

    /**
     * @author β世界 on 19:02 2020/4/27
     * @Description 获取一个字幕组的一部分显示资源
     * 修改Page使用方式
     **/
    @JsonFilter(type = Resource.class,filter = "ani_id,res_id")
    @ResponseBody
    @RequestMapping(value = "/resourceGroup/{sub}",method = RequestMethod.GET)
    public Result<Object> resourceGroup(@PathVariable("sub") Integer sub, int page){
        Map<String,Object> map = new HashMap<>();
        // 设置分页
        Page<Resource> pages = Page.build(subtitleServiceImpl.resourceGroups(sub),page,global.getPageShowNumber());
        map.put("page",pages);
        // 添加字幕组名字
        map.put("subtitleName",subtitleServiceImpl.getSubtitle(sub).getSubtitle_name());
        return ResultUtils.genSuccessResult(map);
    }

    /**
     * @author β世界 on 16:53 2020/6/25
     * 获得一个字幕组的近期更新
     * @param sub: 字幕组的ID
     * @return com.Cet.common.domain.Result
     **/
    @JsonFilter(type = Resource.class,filter = "ani_id,res_id")
    @RequiresUser
    @ResponseBody
    @RequestMapping(value = "/recent/{sub_id}",method = RequestMethod.GET)
    public Result recentUpdate(@PathVariable("sub_id") String sub) throws SubtitleNotExistException {
        // 查询相关的数据
        List<Resource> resources = subtitleServiceImpl.getRecentUpdate(Integer.valueOf(sub));
        // 返回数据信息
        return ResultUtils.genSuccessResult(resources);
    }

    /**
     * @author β世界 on 19:59 2020/4/280
     * @Description 返回所有的字幕组
     **/
    @ResponseBody
    @RequestMapping("/allSubtitle")
    public Result<List<Subtitle>> allSubtitle(){
        return ResultUtils.genSuccessResult(subtitleServiceImpl.getAllSubtitle());
    }

}
